/**
 * 
 */
package org.vsg.stock.core.indicator;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Bill vison
 *
 */
public class MAIndicator implements CycleIndicator {
	
	static private NumberFormat numFor = new DecimalFormat("#.000") ;
	
	
	
	
	/**
	 * defined MA cycle , default value is 5
	 */
	private int cycle = -1;
	

	/**
	 * @return 
	 */
	public int getCycle() {
		return cycle;
	}

	/**
	 * @param cycle
	 */
	public void setCycle(int cycle) {
		this.cycle = cycle;
	}
	


	/* 
	 * @see org.vsg.stock.core.indicator.Indicator#calculate()
	 */
	/**
	 * @return 
	 */
	@Override
	public Map<String, Serializable> calculate() {
		// TODO Auto-generated method stub
		
		double total = 0;
		int tmpCount = 0;
		for (Iterator<Number> dataIter = indicatorModel.iterator() ; dataIter.hasNext(); ) {
			if (tmpCount > this.cycle) {
				break;
			}
			total+= dataIter.next().doubleValue();
			tmpCount++;
		}
		double avg = total / cycle;

		Map<String,Serializable> res = new HashMap<String,Serializable>();

		res.put( Indicator.RESULT , Double.parseDouble( numFor.format( avg ) ));
		
		return res;
	}

	
	private IndicatorModel indicatorModel;

	/**
	 * l(java.util.List)
	 * 
	 * 
	 * @param modelList
	 * @throws CycleException
	 * @t 
	 */
	public void setIndicatorModel(IndicatorModel modelList) throws CycleException{
		
		if (cycle  == -1) {
			throw new CycleException("Indicator Cycle not defined value ! ");
		}
		
		// --- check size --
		if (modelList.size() < cycle) {
			throw new CycleException("Indicator Cycle great than data size! ");
		}
		
		indicatorModel = modelList;		
	}

	/**
	 * l(java.util.List)
	 * 
	 * 
	 * @param modelList
	 * @throws CycleException
	 * @t 
	 */
	public void setIndicatorModel(int modelList) throws CycleException {
	}

}
